package com.smasher.print.em.template;

import android.util.Log;

import com.printsdk.PrintSerializable;
import com.smasher.print.core.PrintField;
import com.smasher.print.core.PrintTemplate;

import org.json.JSONObject;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class TemplatePrintFactory {

    public static final String TAG = "Factory";

    public static PrintTemplate createBryDbTemplate(JSONObject json) {
        EMPrintTemplate template = new EMPrintTemplate();

        template.setTitle("过磅信息卡");
        template.setTitleAlign(PrintSerializable.ALIGN_CENTER);
        template.setTitleFont(new FontConfig(0, 0, 1, 0));
        template.setTitleGap(2);
        template.setFooter("---至臻至善---");
        template.setFooterAlign(PrintSerializable.ALIGN_CENTER);
        template.setFooterFont(new FontConfig(0, 0, 0, 0));
        template.setFooterGap(4);

        List<PrintField> fields = new ArrayList<>();
        // 公司名称
        String company = json.optString("company", "宁夏百瑞源集团");
        EMPrintField companyField = new EMPrintField();
        companyField.setLabel(company);
        companyField.setAlign(PrintSerializable.ALIGN_CENTER);
        companyField.setFont(new FontConfig(0, 0, 0, 0));
        companyField.setValue("");
        companyField.setGap(2);
        fields.add(companyField);

        // 时间
        String pattern = "yyyy-MM-dd HH:mm";
        long time = System.currentTimeMillis();
        SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.getDefault());
        String formatTime = format.format(new Date(time));
        String timeValue = json.optString("time", formatTime);
        EMPrintField timeField = new EMPrintField();
        timeField.setLabel("时间： ");
        timeField.setAlign(PrintSerializable.ALIGN_LEFT);
        timeField.setFont(new FontConfig(0, 0, 0, 0));
        timeField.setValue(timeValue);
        timeField.setGap(1);
        fields.add(timeField);

        // 品种
        String kind = json.optString("kindName", "未知品种");
        EMPrintField kindField = new EMPrintField();
        kindField.setLabel("品种： ");
        kindField.setAlign(PrintSerializable.ALIGN_LEFT);
        kindField.setFont(new FontConfig(0, 0, 0, 0));
        kindField.setValue(kind);
        kindField.setGap(1);
        fields.add(kindField);

        // 品类
        String qua = json.optString("qualityName", "未知品类");
        EMPrintField quaField = new EMPrintField();
        quaField.setLabel("品类： ");
        quaField.setAlign(PrintSerializable.ALIGN_LEFT);
        quaField.setFont(new FontConfig(0, 0, 0, 0));
        quaField.setValue(qua);
        quaField.setGap(1);
        fields.add(quaField);

        // 产地
        String place = json.optString("placeAddress", "未知产地");
        EMPrintField placeField = new EMPrintField();
        placeField.setLabel("产地： ");
        placeField.setAlign(PrintSerializable.ALIGN_LEFT);
        placeField.setFont(new FontConfig(0, 0, 0, 0));
        placeField.setValue(place);
        placeField.setGap(1);
        fields.add(placeField);

        // 批次
        String batch = json.optString("banchno", "未知批次");
        EMPrintField batchField = new EMPrintField();
        batchField.setLabel("批次： ");
        batchField.setAlign(PrintSerializable.ALIGN_LEFT);
        batchField.setFont(new FontConfig(0, 0, 0, 0));
        batchField.setValue(batch);
        batchField.setGap(1);
        fields.add(batchField);

        // 茬数
        String stuage = json.optString("stuageName", "未知茬数");
        EMPrintField stuageField = new EMPrintField();
        stuageField.setLabel("茬数： ");
        stuageField.setAlign(PrintSerializable.ALIGN_LEFT);
        stuageField.setFont(new FontConfig(0, 0, 0, 0));
        stuageField.setValue(stuage);
        stuageField.setGap(1);
        fields.add(stuageField);

        // 车号
        String car = json.optString("carCode", "未知车号");
        EMPrintField carField = new EMPrintField();
        carField.setLabel("车号： ");
        carField.setAlign(PrintSerializable.ALIGN_LEFT);
        carField.setFont(new FontConfig(0, 0, 0, 0));
        carField.setValue(car);
        carField.setGap(1);
        fields.add(carField);

        // 毛重
        String mw = json.optString("mw", "0");
        EMPrintField mwField = new EMPrintField();
        mwField.setLabel("毛重： ");
        mwField.setAlign(PrintSerializable.ALIGN_LEFT);
        mwField.setFont(new FontConfig(0, 0, 0, 0));
        mwField.setValue(mw + "KG");
        mwField.setGap(1);
        fields.add(mwField);

        // 皮重（计算字段）
        String pw = json.optString("pw", "0");
        String pan = json.optString("qw", "0");
        String ksetW = json.optString("bw", "0");

        String tare = calculate(pw, pan, ksetW);
        EMPrintField tareField = new EMPrintField();
        tareField.setLabel("皮重： ");
        tareField.setAlign(PrintSerializable.ALIGN_LEFT);
        tareField.setFont(new FontConfig(0, 0, 0, 0));
        tareField.setValue(tare + "KG");
        tareField.setGap(1);
        fields.add(tareField);

        // 车重
        EMPrintField pwField = new EMPrintField();
        pwField.setLabel("车重： ");
        pwField.setAlign(PrintSerializable.ALIGN_LEFT);
        pwField.setFont(new FontConfig(0, 0, 0, 0));
        pwField.setValue(pw + "KG");
        pwField.setGap(1);
        fields.add(pwField);

        // 盘重
        EMPrintField panField = new EMPrintField();
        panField.setLabel("盘重： ");
        panField.setAlign(PrintSerializable.ALIGN_LEFT);
        panField.setFont(new FontConfig(0, 0, 0, 0));
        panField.setValue(pan);
        panField.setGap(1);
        fields.add(panField);

        // 框重
        EMPrintField ksetWField = new EMPrintField();
        ksetWField.setLabel("框重： ");
        ksetWField.setAlign(PrintSerializable.ALIGN_LEFT);
        ksetWField.setFont(new FontConfig(0, 0, 0, 0));
        ksetWField.setValue(ksetW);
        ksetWField.setGap(1);
        fields.add(ksetWField);

        // 框数
        String kset = json.optString("kset", "0");
        EMPrintField ksetField = new EMPrintField();
        ksetField.setLabel("框数： ");
        ksetField.setAlign(PrintSerializable.ALIGN_LEFT);
        ksetField.setFont(new FontConfig(0, 0, 0, 0));
        ksetField.setValue(kset);
        ksetField.setGap(1);
        fields.add(ksetField);

        // 净重
        String jw = json.optString("jw", "0");
        EMPrintField jwField = new EMPrintField();
        jwField.setLabel("净重： ");
        jwField.setAlign(PrintSerializable.ALIGN_LEFT);
        jwField.setFont(new FontConfig(0, 0, 0, 0));
        jwField.setValue(jw + "KG");
        jwField.setGap(2);
        fields.add(jwField);

        template.setFields(fields);
        return template;
    }

    private static String calculate(String pw, String pan, String ksetW) {
        BigDecimal result = BigDecimal.ZERO;
        try {
            result = new BigDecimal(pw).add(new BigDecimal(pan)).add(new BigDecimal(ksetW));
        } catch (Exception e) {
            Log.e(TAG, "calculate: ", e);
        }
        return String.valueOf(result.floatValue());
    }
}
